package it.koutifaris.travianmap.ui.touch;

import it.koutifaris.travianmap.context.HasApplicationContex;
import it.koutifaris.travianmap.map.WorldMap;
import it.koutifaris.travianmap.model.Village;
import it.koutifaris.travianmap.ui.MapSource;
import it.koutifaris.travianmap.ui.viewstate.DynamicViewState;
import it.koutifaris.travianmap.ui.viewstate.ViewStateSource;
import it.koutifaris.travianmap.ui.viewstate.XYSquare;
import it.koutifaris.travianmap.view.distances.DistancesActivity;
import it.koutifaris.travianmap.view.villagedetails.VillageDetailsDialog;
import android.content.Context;
import android.os.Vibrator;
import android.support.v4.app.FragmentActivity;

public class MapTouchHandler extends HasApplicationContex implements OnVillageTouchHandler {

	private static final String TAG = MapTouchHandler.class.getSimpleName();
	private final ViewStateSource viewStateSource;
	private final MapSource mapSource;

	public MapTouchHandler(ViewStateSource viewStateSource, MapSource mapSource) {
		this.viewStateSource = viewStateSource;
		this.mapSource = mapSource;
	}

	@Override
	public void onVillageTapped(Village tappedVillage) {
		if (touchOnSelectedVillage(tappedVillage, getViewState().getSelectedSquare())) {
			clearSelection();
		} else if (getViewState().getSelectedSquare() != null) {
			showDistanceDetails(tappedVillage);
		} else {
			showVillageDetails(tappedVillage);
		}
	}

	private void showDistanceDetails(Village tappedVillage) {
		XYSquare selectedSquare = getViewState().getSelectedSquare();
		Village from = getSelectedVillage(selectedSquare);
		Village to = tappedVillage;

		showDistancesActivity(from, to);
	}

	private void showDistancesActivity(Village from, Village to) {
		DistancesActivity.startActivity(from, to);
	}

	private Village getSelectedVillage(XYSquare selectedSquare) {
		return getMap().getSquareAt(selectedSquare.getX(), selectedSquare.getY()).getVillage();
	}

	private WorldMap getMap() {
		return mapSource.getMap();
	}

	private void clearSelection() {
		getViewState().clearSelectedSquare();
	}

	private void showVillageDetails(Village tappedVillage) {
		FragmentActivity act = (FragmentActivity) getActivity();
		VillageDetailsDialog villageDetailsDialog = new VillageDetailsDialog();
		VillageDetailsDialog.storeParameters(tappedVillage);
		villageDetailsDialog.show(act.getSupportFragmentManager(), TAG);
	}

	@Override
	public void onVillageLongPress(Village pressedVillage) {
		vibrate();

		if (!touchOnSelectedVillage(pressedVillage, getViewState().getSelectedSquare())) {
			getViewState().setSelectedSquare(pressedVillage.getX(), pressedVillage.getY());
		} else {
			clearSelection();
		}
	}

	private DynamicViewState getViewState() {
		return viewStateSource.getViewState();
	}

	private void vibrate() {
		Vibrator vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
		vibrator.vibrate(50);
	}

	private boolean touchOnSelectedVillage(Village pressedVillage, XYSquare selectedSquare) {
		if (selectedSquare == null) {
			return false;
		}

		return (pressedVillage.getX() == selectedSquare.getX()) && (pressedVillage.getY() == selectedSquare.getY());
	}
}
